<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>门店设备配置</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页
    <span class="c-gray en">&gt;</span>
    门店管理
    <span class="c-gray en">&gt;</span>
    门店设备配置
</nav>
<div class="page-container">
    <div id="merchantStoreEquipmentToolbar">
        <a href="javascript:;" onclick="merchantStoreEquipmentFun.delAll();" class="btn btn-danger radius">
            <i class="Hui-iconfont">&#xe6e2;</i> 批量删除
        </a>
        <a class="btn btn-primary radius" data-title="添加设备" data-href="merchant/store/equipment_add.html"
           onclick="Hui_admin_tab(this)" href="javascript:;">
            <i class="Hui-iconfont">&#xe600;</i>
            添加设备
        </a>
    </div>
    <div class="mt-20">
        <table id="store-equipment" class="table table-border table-bordered table-bg table-hover table-sort">
        </table>
    </div>
</div>
<!--***************修改的模态框***********-->
<div id="merchantEquipmentModal" class="modal fade merchant-equipment-edit-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">修改设备</h4>
            </div>
            <div class="modal-body">
                <form id="edit-form-equipment" action="" method="post" class="form form-horizontal">
                    <input type="text" class="form-control" value="" id="StoreEquipment-equipmentId" name="equipmentId" style="display: none;">
                    <div class="form-group">
                        <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>门店名称：</label>
                        <div class="formControls col-xs-8 col-sm-8">
                            <input type="text" class="form-control" value="" placeholder="必填项" id="StoreEquipment-storeName" name="storeName">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>门店编号：</label>
                        <div class="formControls col-xs-8 col-sm-8">
                            <input type="text" class="form-control" placeholder="必填项" id="StoreEquipment-storeNumber" name="storeNumber">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>设备名称：</label>
                        <div class="formControls col-xs-8 col-sm-8">
                            <input type="text" class="form-control" value="" placeholder="必填项" id="StoreEquipment-equipmentName" name="equipmentName">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-4 col-sm-3"><span class="c-red"></span>设备数量：</label>
                        <div class="formControls col-xs-8 col-sm-8">


                            <input type="text" class="form-control" value="" placeholder="必填项" id="StoreEquipment-equipmentNumber" name="equipmentNumber">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="merchantStoreEquipmentFun.modify()">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    /*服务类型配置 merchantStoreEquipmentFun*/
    var merchantStoreEquipmentFun;

    merchantStoreEquipmentFun = {
        init: function () {
            var _this = this;
            _this.bootstrapvalidatorForm();

            $('#store-equipment').bootstrapTable({
                url: 'biz/getServiceByBizId',         //请求后台的URL（*）
                method: 'get',
                toolbar: '#merchantStoreEquipmentToolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: false,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 10,                       //每页的记录行数（*）
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                //search: true,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: true,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                // height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
                showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                columns: [
                    {
                        checkbox: true
                    }, {
                        field: 'storeId',
                        title: '门店id',
                        visible: false
                    }, {
                        field: 'equipmentId',
                        title: '设备id',
                        visible: false
                    },
                    {
                        field: 'storeName',
                        title: '门店名称'
                    }, {
                        field: 'storeNo',
                        title: '门店编号'
                    }, {
                        field: 'equipmentName',
                        title: '设备名称'
                    }, {
                        field: 'equipmentNumber',
                        title: '设备数量'
                    }, {
                        field: 'address',
                        title: '操作',
                        formatter: function (value, row, index) {
                            return `
                        <a class="btn btn-info btn-sm" type="button"
                         data-toggle="modal"
                         data-target=".merchant-equipment-edit-modal"
                          onclick="merchantStoreEquipmentFun.findOne('${row.equipmentId}')"
                         >修改
                        </a>
                        <a class="btn btn-danger btn-sm" onclick="merchantStoreEquipmentFun.del_array('${row.equipmentId}')">删除
                        </a>
                    `;
                        }
                    }
                ]
            });
        },
        refresh: function () {
            $('#store-equipment').bootstrapTable("refresh");
        },
        //删除
        del_array: function (index) {
            layer.confirm('确认要删除吗？', {btn: ['确定', '取消']},
                function () {
                    //确认删除
                    $.ajax({
                        type: "GET",
                        url: "biz/deleteServiceTypeByBizId",
                        data: {equipmentId: index},
                        dataType: "json",
                        success: function (data) {
                            if (data.state) {
                                layer.msg(data.message, {icon: 1});
                                merchantStoreEquipmentFun.refresh();
                            }
                        },
                        error: function () {
                            alert("error");
                        }
                    });
                },
                function () {
                    layer.msg('已取消！', {icon: 1});
                    return false;
                }
            )
        },
        //批量删除
        delAll: function () {
            let _this = this;
            layer.confirm('确认要删除吗？',
                {
                    btn: ['确定','取消']
                },
                function(){
                    //确认删除
                    //获取所有被选中的记录
                    var rows = $("#store-equipment").bootstrapTable('getSelections');
                    if (rows.length== 0) {
                        layer.alert("请先选择要删除的记录!");
                        return;
                    }
                    var ids = '';
                    for (var i = 0; i < rows.length; i++) {
                        ids += rows[i]['equipmentId'] + ",";
                    }
                    ids = ids.substring(0, ids.length - 1);

                    //删除
                    $.ajax({
                        type: "get",
                        url: "biz/deleteAllByBizId",
                        data:{list:ids},
                        dataType: "json",
                        success : function(data) {
                            if(data.state){
                                layer.msg(data.message, {icon: 1});
                                //涮新一次
                                $('#store-equipment').bootstrapTable("refresh");

                                return true;
                            }else {
                                layer.msg("批量删除失败！", {icon: 2});
                                return false;
                            }

                        },
                        error:function () {
                            alert("error");
                        }
                    });

                },
                function(){
                    layer.msg('已取消！', {icon: 1});
                    return false;
                }
            );
        },
        //查询单条，修改
        findOne: function (index) {
            var _this = this;
            _this.clearInput();

            $.ajax({
                type: "GET",
                url: "biz/getOneServiceByBizId",
                data: {equipmentId: index},
                dataType: "json",
                success: function (ret) {
                    if (ret.state) {
                        let retData = ret.data;
                        $("#StoreEquipment-equipmentName").val(retData.equipmentName);
                        $("#StoreEquipment-storeName").val(retData.storeName);
                        $("#StoreEquipment-storeNumber").val(retData.storeNumber);
                        $("#StoreEquipment-equipmentNumber").val(retData.equipmentNumber);
                        $("#StoreEquipment-equipmentId").val(retData.equipmentId);
                        var ddd = $("#StoreEquipment-equipmentType");    //获取selectid
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },
        //修改
        modify: function () {
            //bootstrap验证
            var bootstrapValidator = $("#edit-form-equipment").data('bootstrapValidator');
            bootstrapValidator.validate();
            if(bootstrapValidator.isValid())
                $("#edit-form-equipment").submit();
            else return;

            var temp = $("#edit-form-equipment").serializeArray();

            $.ajax({
                type: "POST",
                url: "biz/service/edit",
                data: temp,
                dataType: "json",
                success: function (ret) {
                    layer.msg(ret.message, {icon: 1});
                    if (ret.state) {
                        $("#merchantEquipmentModal").modal('hide');
                        merchantStoreEquipmentFun.refresh();
                    }
                },
                error: function () {
                    alert("error");
                }
            });
        },

        //清空文本框
        clearInput: function () {
            $("#StoreEquipment-equipmentName").val();
            $("#StoreEquipment-storeName").val();
            $("#StoreEquipment-storeNumber").val();
            $("#StoreEquipment-equipmentNumber").val();
            $("#StoreEquipment-equipmentId").val();

            //重置validator
            $("#edit-form-equipment").data('bootstrapValidator').resetForm();
        },
        //bootstrap验证
        bootstrapvalidatorForm: function () {
            $('#edit-form-equipment').bootstrapValidator({
                message: 'This value is not valid',
                excluded : [':disabled'],
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    storeName: {
                        validators: {
                            notEmpty: {
                                message: '门店名称不能为空'
                            },
                        }
                    },
                    storeNumber: {
                        validators: {
                            notEmpty: {
                                message: '门店编号不能为空'
                            },
                        }
                    },
                    equipmentName: {
                        validators: {
                            notEmpty: {
                                message: '设备名称不能为空'
                            },
                            stringLength: {
                                min: 2,
                                max: 100,
                                message: '设备名称长度必须在2到100位之间'
                            }
                        }
                    },
                    equipmentNumber: {
                        validators: {
                            notEmpty: {
                                message: '设备数量不能为空'
                            },
                        }
                    },
                }
            });
        }
    };

    merchantStoreEquipmentFun.init();
</script>
</body>
</html>